#include "XDecodeThread.h"
#include "XDecode.h"
#include "qdebug.h"
XDecodeThread::XDecodeThread()
{
	if (!decode) {
		qDebug() << "create decode";
		decode = new XDecode();
	}
}

XDecodeThread::~XDecodeThread()
{

}
void XDecodeThread::Push(AVPacket * pkt)
{
	if (!pkt) {
		qDebug() << "XAudioThread::Push pkt is NULL";
		return;
	}
	//进行阻塞
	while (!isExit) {
		mux.lock();
		if (packs.size() < maxList) {
			packs.push_back(pkt);
			mux.unlock();
			break;
		}
		mux.unlock();
		msleep(1);
	}
}

AVPacket * XDecodeThread::Pop()
{
	mux.lock();
	if (packs.empty())
	{
		mux.unlock();
		return NULL;
	}
	AVPacket *pkt = packs.front();
	packs.pop_front();
	mux.unlock();
	return pkt;
}

void XDecodeThread::Clear()
{
	mux.lock();
	decode->Clear();
	while (!packs.empty())
	{
		AVPacket *pkt = packs.front();
		XFreePacket(&pkt);
		packs.pop_front();
	}
	mux.unlock();
}

void XDecodeThread::Close()
{
	Clear();
	isExit = true;
	wait();
	decode->Close();

	mux.lock();
	delete decode;
	decode = NULL;
	mux.unlock();
}
